﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Color = System.Windows.Media.Color;

#endregion

namespace ScrumTable.UI.View.Util
{

    /// <summary>
    /// BitmapUtil
    /// </summary>
    public class BitmapUtil
    {
        private static int _margin = 10;

        #region Methods

        public static BitmapSource CreateBitmapSource(Bitmap bitmap)
        {
           
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            BitmapSource source = BitmapSource.Create(bitmap.Width, bitmap.Height,
                bitmap.HorizontalResolution, bitmap.VerticalResolution, PixelFormats.Pbgra32,
                                                      null,
                                                      bitmapData.Scan0, bitmapData.Stride * bitmap.Height,
                                                      bitmapData.Stride);
            
            bitmap.UnlockBits(bitmapData);
            if (source.CanFreeze)
            {
                source.Freeze();
            }
            return source;
        }


        public static BitmapSource CreateBitmapSource(Bitmap bitmap, int x, int y, int width, int height)
        {
            BitmapSource source = CreateBitmapSource(bitmap);
            source = new CroppedBitmap(source, new Int32Rect(x, y, Math.Min(width, bitmap.Width)-x, Math.Min(bitmap.Height, height)-y));
            if (source.CanFreeze)
            {
                source.Freeze();
            }
            return source;
        }


        
        /// <summary>
        /// Gets the width of a bitmap without the White border (on the right side)
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static int GetWidth(Bitmap bitmap)
        {
            for (int u = bitmap.Width - _margin - 1; u > 0; u -= 5)
            {
                for (int i = 0; i < bitmap.Height; i+=2)
                {
                    var color = bitmap.GetPixel(u, i);
                    if (color.R != 255 && color.B != 255 && color.G != 255)
                    {
                        return u + _margin;
                    }
                }
            }
            return bitmap.Width;
        }
 
        /// <summary>
        /// Gets the width of a bitmap without the White border (on the right side)
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static int GetStartX(Bitmap bitmap)
        {
            for (int u = _margin; u < bitmap.Width ; u += 5)
            {
                for (int i = 0; i < bitmap.Height; i += 2)
                {
                    var color = bitmap.GetPixel(u, i);
                    if (color.R != 255 && color.B != 255 && color.G != 255)
                    {
                        return u - _margin;
                    }
                }
            }
            return bitmap.Width;
        }

        /// <summary>
        /// Gets the height of a bitmap without the White border (on the bottom)
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="width">height caluclated by GetWidth</param>
        /// <returns></returns>
        public static int GetStartY(Bitmap bitmap, int width)
        {
            for (int u = _margin; u < bitmap.Height; u += 5)
            {
                for (int i = 0; i < width; i += 2)
                {
                    var color = bitmap.GetPixel(i, u);
                    if (color.R != 255 && color.B != 255 && color.G != 255)
                    {
                        return u - _margin;
                    }
                }
            }
            return bitmap.Height;
        }
        

        /// <summary>
        /// Gets the height of a bitmap without the White border (on the bottom)
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="width">height caluclated by GetWidth</param>
        /// <returns></returns>
        public static int GetHeight(Bitmap bitmap, int width)
        {
            for (int u = bitmap.Height - _margin - 1; u > 0; u -= 5)
            {
                for (int i = 0; i < width; i += 2)
                {
                    var color = bitmap.GetPixel(i, u);
                    if (color.R != 255 && color.B != 255 && color.G != 255)
                    {
                        return u + _margin;
                    }
                }
            }
            return bitmap.Height;
        }
        #endregion
    }
}
